package com.wzcl.app.service.impl;

import com.wzcl.app.dao.model.AppUser;
import com.wzcl.app.dao.model.TestPaper;
import com.wzcl.app.dao.model.TestPaperDownload;
import com.wzcl.app.dao.model.TopUpOrder;
import com.wzcl.app.dao.repository.CommodityRepository;
import com.wzcl.app.dao.repository.TopUpOrderRepository;
import com.wzcl.app.dao.repository.CourseRepository;
import com.wzcl.app.model.common.MyPage;
import com.wzcl.app.model.dto.TestPaperDownloadDto;
import com.wzcl.app.model.dto.TopUpOrderDto;
import com.wzcl.app.service.TopUpOrderService;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;

/**
 * @author beingven
 * @Description
 * @createTime 2020年11月26日 13:36:00
 */
@Service
public class TopUpOrderServiceImpl implements TopUpOrderService {

    @Resource
    private TopUpOrderRepository topUpOrderRepository;

    @Override
    public MyPage<List<TopUpOrder>> selectByCondition(TopUpOrderDto topUpOrderDto) {
        return new MyPage<>(topUpOrderRepository.findAll(getSpecification(topUpOrderDto), topUpOrderDto.getPageable()));
    }

    //条件筛选
    public Specification<TopUpOrder> getSpecification(TopUpOrderDto topUpOrderDto){
        return (Specification<TopUpOrder>) (root, criteriaQuery, criteriaBuilder) -> {
            ArrayList<Predicate> list = new ArrayList<>();
            //条件过滤
            if (topUpOrderDto.getOrderNumber() != null && topUpOrderDto.getOrderNumber() != ""){
                //订单号模糊查询
                list.add(criteriaBuilder.like(root.get("orderNumber"), "%"+topUpOrderDto.getOrderNumber()+"%"));
            }
            if (topUpOrderDto.getUserPhone() != null && topUpOrderDto.getUserPhone() != ""){
                list.add(criteriaBuilder.like(root.get("userPhone"), "%"+topUpOrderDto.getUserPhone()+"%"));
            }
            // 排序
            criteriaQuery.orderBy(criteriaBuilder.desc(root.get("createTime")));
            //将两个查询条件联合起来之后返回Predicate对象
            return criteriaBuilder.and(list.toArray(new Predicate[list.size()]));
        };
    }
}


